# 架构平台
ARCH = amd64
OS = windows

# 项目路径变量
DIR := $(shell pwd)
OUTPUT = main

# 镜像容器变量
CONTAINER_NAME = refactor_mall
IMAGE_NAME = gin_mall:3.0

# Go 构建相关变量
GO = go
GO_BUILD = $(GO) build
GO_BUILD_FLAGS = -v
GO_BUILD_LDFLAGS = -X main.version=$(VERSION)

.PHONY: build
build:  ## 构建项目
	@echo "Building project to ./$(OUTPUT)"
	$(GO_BUILD) \
	$(GO_BUILD_FLAGS) -ldflags "$(GO_BUILD_LDFLAGS)" \
	-o ./$(OUTPUT) ./cmd

.PHONY: run
run: build  ## 构建并运行
	@./$(OUTPUT)

.PHONY: env-up
env-up:  ## 启动 Redis 和 MySQL 环境
	docker-compose up -d redis mysql
	@echo "Redis & MySQL 启动完成"

.PHONY: env-down
env-down:  ## 关闭 Redis 和 MySQL 环境
	docker-compose down
	@echo "Redis & MySQL 停止完成"

.PHONY: docker-up
docker-up:  ## 构建镜像并以容器方式运行
	docker build \
	-t $(IMAGE_NAME) \
	-f ./Dockerfile \
	./
	docker run \
	-it \
	--name $(CONTAINER_NAME) \
	--network host \
	-d $(IMAGE_NAME)
	@echo "容器部署成功，运行在 localhost:5001"

.PHONY: docker-down
docker-down:  ## 停止并删除容器和镜像
	docker stop $(CONTAINER_NAME)
	docker rm $(CONTAINER_NAME)
	docker rmi $(IMAGE_NAME)
	@echo "容器删除成功"

default: run
